package com.spring.gateway;





import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;


import reactor.core.publisher.Mono;

/**
 * 过滤器
 * @author zzx
 *
 */
@Component
public class WrapperResponseFilter implements GlobalFilter, Ordered{	
	
	
	@Override
	public int getOrder() {		
		return 0;
	}
	
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		ServerHttpRequest request = exchange.getRequest();
		String strUrl = request.getURI().toString();
        System.err.println("**method**"+ request.getMethod());
        System.err.println("**strUrl**"+ strUrl);
        Object name = request.getQueryParams().get("name");
        System.err.println("***name****"+name);
        System.err.println("**************过滤器**************");        
        System.out.println(" uri : " + strUrl);//打印每次请求的url
        String token = request.getQueryParams().getFirst( "token" );
        if ( StringUtils.isBlank( token )) {
           System.err.println( "token is empty ..." );
            exchange.getResponse().setStatusCode( HttpStatus.UNAUTHORIZED );// HttpStatus.UNAUTHORIZED 401
            return exchange.getResponse().setComplete();
        }
       // exchange.getAttributes().
        //exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, new URI("http://192.168.31.245:8081/house/hello2"));
      
        return chain.filter(exchange);
	}	
}
